#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"../include/main.h"
#include"../include/bst.h"
#include"../include/create_file.h"
#include"../include/mount_vfs.h"
#include"../include/narrtree.h"
#include "../include/hash.h"
#include"../include/vfs_errorcodes.h"
#include"../include/create_vfs.h"

void addfile(char path[],char filename[],char filepath_data[])
{
	fdesc *mkdesc,*root;
	FILE *fp;
	size_t size;
	char temp[MAX_PATH],*t,invalid_char[]={'/'};
	char *k;
	llist *list;
	int i=0,j=0,counter=1,ft=0;
	if(mounted==0)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_ADDFILE_07);
	else if((t=strchr(filename,invalid_char[i])) != NULL)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_ADDFILE_02);
	else if(local_max_fd > local_used_fd)
	{
		i=0;
		while(path[i] != '\0')
		{
			temp[i]=path[i];
			i++;
		}
		temp[i]='\0';
		k=strrchr(temp,'/');
		if(k!=NULL)
		{
			k++;
			if(*k != '\0')
		        strcat(temp,"/");
		}
		//strcat(temp,"/");
		strcat(temp,filename);
		mkdesc=(fdesc *)malloc(sizeof(fdesc));
		strcpy(mkdesc->filepath,temp);
		strcpy(mkdesc->filename,filename);
		mkdesc->filetype=0;
		fp=fopen(filepath_data,"r");
		if(fp==NULL)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_ADDFILE_05);
		else
		{
			fseek(fp,0L,SEEK_END);
			ft=ftell(fp);
			fseek(fp,0,SEEK_SET);
			//size = fread(data[glb_blk_num],DATA_SIZE,1,fp);
			//printf("in addfile ft=%d\n",ft);
			if(ft > BLOCK_SIZE)
			{
				printf("%s_FAILURE %s\n",glb_command,ERR_VFS_ADDFILE_06);
				strcpy(data[glb_blk_num]," ");
			}
			else
			{
				//size = fread(data[glb_blk_num],DATA_SIZE,1,fp);
				if(local_used_fd == 0)
				{
					root=(fdesc *)malloc(sizeof(fdesc));
					strcpy(root->filepath,"/");
					//root->filepath[strlen(root->filepath)-1]='\0';
					strcpy(root->filename,"/");
					//root->filename[strlen(root->filename)-1]='\0';
					root->filetype=1;
					root->file_size=0;
					root->blk_num=0;
					head=create(root);
					list=insert(root);
					local_used_fd++;
					mkdesc->blk_num=glb_blk_num;
					*(block_arr+glb_blk_num)=glb_blk_num;
					size = fread(data[glb_blk_num],DATA_SIZE,1,fp);
					data[glb_blk_num][strlen(data[glb_blk_num])]='\0';
					mkdesc->file_size=strlen(data[glb_blk_num]);
					head=insert_ntree(head,mkdesc);
					local_used_fd++;
					printf("addfile_SUCCESS\n");
				}
				else
				{
					if(glb_blk_num >= local_max_fd)
					{
						while(*(block_arr+counter) != 0 && *(block_arr+counter) < size_of_vfs && counter < (size_of_vfs-1))
							counter++;
						if(counter < (size_of_vfs-1))
						{
							mkdesc->blk_num=counter;
							*(block_arr+counter)=counter;
						}
						size = fread(data[counter],DATA_SIZE,1,fp);
						data[counter][strlen(data[counter])]='\0';
						mkdesc->file_size=strlen(data[counter]);
					}
					else
					{
						mkdesc->blk_num=glb_blk_num;
						//printf("addfile mkdesc->filepath=%s   mkdesc->blk_num==%d\n",mkdesc->filepath,mkdesc->blk_num);
						*(block_arr+glb_blk_num)=glb_blk_num;
			
						size = fread(data[glb_blk_num],DATA_SIZE,1,fp);
						data[glb_blk_num][strlen(data[glb_blk_num])]='\0';
						mkdesc->file_size=strlen(data[glb_blk_num]);
					}
				
		
					//block_arr++;
					list=insert(mkdesc);
					if(list != NULL)
					{
						data_num++;
						//printf("data[%d]%s\n",glb_blk_num,data[glb_blk_num]);
						glb_blk_num++;
						head=insert_ntree(head,mkdesc);
						//traverse(head);
						local_used_fd++;
						printf("addfile_SUCCESS\n");
						//printf("local_max_fd=%d local_used_fd=%d\n",local_max_fd,local_used_fd);
					}
					else
						printf("%s_FAILURE %s\n",glb_command,ERR_VFS_ADDFILE_03);
				}
			}
		}
	}
	else
	{
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_ADDFILE_04);
		//printf("%s\n",ERR_VFS_ADDFILE_01);
	}
	if(fp != NULL)
	close(fp);
	//printf("data_num==%d   glb_blk_num==%d\n",data_num,glb_blk_num);
}
